package games.gong.durid.mapper;

import games.gong.durid.entity.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PermissionMapper {
    
    @Insert("INSERT INTO permission(name, description, resource, action, create_time, update_time) " +
            "VALUES(#{name}, #{description}, #{resource}, #{action}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Permission permission);

    @Delete("DELETE FROM permission WHERE id = #{id}")
    int deleteById(Long id);

    @Update("UPDATE permission SET name=#{name}, description=#{description}, resource=#{resource}, " +
            "action=#{action}, update_time=#{updateTime} WHERE id = #{id}")
    int update(Permission permission);

    @Select("SELECT * FROM permission WHERE id = #{id}")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    Permission selectById(Long id);

    @Select("SELECT * FROM permission")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<Permission> selectAll();
    
    // 根据角色ID查询权限
    @Select("SELECT p.* " +
            "FROM permission p " +
            "INNER JOIN role_permission rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId}")
    @Results({
        @Result(property = "createTime", column = "create_time"),
        @Result(property = "updateTime", column = "update_time")
    })
    List<Permission> selectPermissionsByRoleId(Long roleId);
}